/************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     pic.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-01
  * @brief    
 *************************************************************************/

#include "pit.h"
#include <string.h>
#include "sys.h"
#include "function_drv.h"
#include "eport_drv.h"
#include "tp_ctp.h"
#include "pit32_drv.h"
#include "bsp.h"
#include "wav_dac.h"
#include "uart.h"

uint8_t flag_MB_REG[120];

#if (UARTBUS_OPTION == 0 || UARTBUS_OPTION == 3 || UARTBUS_OPTION == 4 ||  \
	UARTBUS_OPTION == 5 || UARTBUS_OPTION == 6)

// timer for dynamic display
void PIT1_Init(void)
{
	NVIC_Init(1, 0, PIT1_IRQn, 2);

	PIT1->PCSR &= (~PCSR_EN);
	PIT1->PCSR = (5 << 8) | PCSR_OVW | PCSR_PIE | PCSR_RLD | PCSR_PDBG; // pit period = 2^PRE��3:0�ݡ���PM��31:0��+1���� system clock period
	PIT1->PMR = 23437;													// 10ms = 2^5 x (23437+1) x (1/75000kHZ)
	PIT1->PCSR |= PCSR_EN;
	
}

/*void sent_CMD(uint8_t num)
{
	uint8_t rebuf[5];

	switch(num)
	{
		case 50:
			rebuf[0] = 0x60;//查音量
			rebuf[1] = 0x01;
			rebuf[2] = 0xC0;	
			LT_SendData_CRC_Frame(rebuf, 3);
		break;
		
		case 100:		
			rebuf[0] = 0x60;//查麦克风
			rebuf[1] = 0x02;
			rebuf[2] = 0xC0;	
			LT_SendData_CRC_Frame(rebuf, 3);
		break;

		case 150:
			rebuf[0] = 0x60;//查模式
			rebuf[1] = 0x06;
			rebuf[2] = 0xC0;	
			LT_SendData_CRC_Frame(rebuf, 3);
		break;		
		case 200:
			rebuf[0] = 0x60;//查静音
			rebuf[1] = 0x08;
			rebuf[2] = 0xC0;	
			LT_SendData_CRC_Frame(rebuf, 3);
		break;
		
	}
}*/

uint16_t send500_time = 0;
uint16_t zhizhen_delay = 0;
int tempA,tempB,tempC;
void PIT1_IRQHandler(void)
{
	uint8_t rebuf[5];
	PIT1->PCSR |= (1 << 2); // clear PIF interrupt flag
	
	/*sent_time++;
	if(sent_time==50||sent_time==100||sent_time==150)
		sent_CMD(sent_time);
	else if(sent_time>150)
		sent_time = 0;*/
	
		if(send500_time>0)
		{
			send500_time++;
			if(send500_time>50)//500ms响应改变
			{
				send500_time = 0;
				tempA = pic_flagA;
				tempB = pic_flagB;
				tempC = pic_flagC;
			}
		}
	
		zhizhen_delay++;
		if(zhizhen_delay>20)//200ms发一次
		{
			zhizhen_delay = 0;
			
		    rebuf[0] = 0xAA;
			rebuf[1] = Ye_Mian_num+1;
			rebuf[2] = tempA&0xff;
			rebuf[3] = tempB&0xff;
			rebuf[4] = tempC&0xff;	
			LT_SendData_CRC_Frame(rebuf, 5);
		}


	if (Var_Count_num > 0)
		Var_Count_flag = 1;
	if(Pointer_Count_num >0)
		Pointer_Count_flag = 1;
	if (Gif_num > 0)
		Gif_flag = 1;
	if (Adj_loop > 0)
		Adj_time++;
	if (DioKb_flag == 1)
		Cusor_time++;
	if (AioKb_flag == 1)
		Cusor_time++;
	if (GBKioKb_flag == 1)
		Cusor_time++;
	if (Scroll_num > 0)
		Scroll_flag = 1;
	if (curve_num > 0)
		curve_flag = 1;
	if (Backlight_flag)
		Backlight_count++;
	if (buzzer_start)
	{
		buzzer_10ms_count++;
		if (buzzer_10ms_count >= 5)
		{
			touch_buzzer_close();
			buzzer_start = 0;
			buzzer_10ms_count = 0;
		}
	}
#if (LT_TOUCH_FLAG == 1)
	if (RTP_read_Flag == 1)
	{
		RTP_time++;
		if (RTP_time > 5)
		{
			RTP_read_Flag = 0;
			RTP_time = 0;
		}
	}
#endif
	page_refreshing++;
	if(ctp_press_t < 0xFF) ctp_press_t++;

#if LT_ENCODER
	if (SW_flag4 != 2 || SW_flag4 != 3)
		key_time++;

	if (Encoder_read_Flag == 1)
	{
		Backlight_count = 0;
		if (Encoder_type == 0)
		{
			Encoder_time++;
			if (Encoder_time > 1)
			{
				if (Read_Encoder_key == 1)
				{
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
					SW_flag = 1;
				}
			}
		}
		else if (Encoder_type == 4)
		{
			Encoder_time++;
			if (Encoder_time > 1)
			{
				if (SW_flag4 == 1)
				{
					SW_flag4 = 2;
					SW_flag = 3;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
				if (Encoder_time > (Long_press * 5) && SW_flag4 != 3)
				{
					SW_flag4 = 3;
					SW_flag = 4;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
				if (Read_Encoder_key == 1)
				{
					SW_flag4 = 1;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
			}
		}
	}
	if (SW_flag4 == 1)
	{
		Encoder_time++;
		if (Encoder_time > Double_click_interval * 5) // Double-click valid time exceeded
		{
			SW_flag4 = 1;
			SW_flag = 2;
		}
	}
	if (Enc1)
	{
		Encoder_Freetime++;
		if (Encoder_Freetime > 100)
		{
			Encoder_Freetime = 0;
			knob_dir = 0;
			Enc1 = 0;
		}
	}
#endif
}
#endif


volatile uint8_t Addr_ModBus_Sla = 0x01;

#if (UARTBUS_OPTION == 1)

void PIT1_Init(void)
{
	NVIC_Init(1, 0, PIT1_IRQn, 2);

	PIT1->PCSR &= (~PCSR_EN);
	PIT1->PCSR = (3 << 8) | PCSR_OVW | PCSR_PIE | PCSR_RLD | PCSR_PDBG; // 75mhz/8  Interrupt mode
	PIT1->PMR = 9375;													// 1ms
	PIT1->PCSR |= PCSR_EN;
}

#define MB_ERR_CMD 0x01
#define MB_ERR_ADDR 0x02
#define MB_ERR_DATA 0x03
#define MB_ERR_FAULT 0x04

volatile MB_CMD_RXINFO gMB_CMDRX;
volatile uint8_t MB_SLA_RXBUF[256] = {0};
volatile uint16_t MB_SLA_Len = 0;
//volatile uint8_t Addr_ModBus_Sla = 0x01;
volatile uint8_t Feedback_ModbusBuf[256] = {0};

volatile uint32_t pit_1ms_count = 0;
volatile uint8_t uart_flag = 0;
volatile uint32_t pit_1ms_count2 = 0;

void PIT1_IRQHandler(void)
{
	uint16_t CRC_MCU = 0, CRC_RX = 0;
	uint16_t i = 0, j = 0;
	uint8_t num = 0, cnt = 0;
	uint16_t temp = 0;
	PIT1->PCSR |= PCSR_PIF; // clear PIF interrupt flag
	pit_1ms_count++;
	pit_1ms_count2++;
	// If the serial port is detected for 5ms and no data is received, the reception is completed
	if (pit_1ms_count == 5 && uart_flag)
	{
		pit_1ms_count = 0;
		uart_flag = 0;

		MB_SLA_Len = gUsartRx.Count;
		memcpy(MB_SLA_RXBUF, gUsartRx.Buf, MB_SLA_Len);
		gUsartRx.Count = 0;


		if (MB_SLA_RXBUF[0] == Addr_ModBus_Sla || MB_SLA_RXBUF[0] == 0) // Transceiver node consistency or broadcast mode
		{
			CRC_MCU = (MB_SLA_RXBUF[MB_SLA_Len - 1] << 8) + (MB_SLA_RXBUF[MB_SLA_Len - 2] & 0x00FF);
			CRC_RX = CRC16(MB_SLA_RXBUF, MB_SLA_Len - 2);
			if (CRC_MCU == CRC_RX) // CRC check is correct
			{
				gMB_CMDRX.CMD = MB_SLA_RXBUF[1];
				gMB_CMDRX.Addr = MB_SLA_RXBUF[2] << 8 | MB_SLA_RXBUF[3];

				Var_AddrRemapping(&gMB_CMDRX.Addr);

				if (gMB_CMDRX.CMD == 0x03 && MB_SLA_RXBUF[0] != 0) // Read command response (broadcast mode read function is meaningless)
				{
					gMB_CMDRX.Num = MB_SLA_RXBUF[4] << 8 | MB_SLA_RXBUF[5];
					if (gMB_CMDRX.Addr < VAR_ADDR_S) // Variable address
					{
						Feedback_ModbusBuf[0] = Addr_ModBus_Sla;
						Feedback_ModbusBuf[1] = gMB_CMDRX.CMD;
						Feedback_ModbusBuf[2] = 2 * gMB_CMDRX.Num;
						for (i = 0; i < gMB_CMDRX.Num; i++)
						{
							Feedback_ModbusBuf[3 + 2 * i] = var[2 * (gMB_CMDRX.Addr + i)];
							Feedback_ModbusBuf[4 + 2 * i] = var[2 * (gMB_CMDRX.Addr + i) + 1];
						}
						LT_SendData_CRC_NoFrame(Feedback_ModbusBuf, 3 + Feedback_ModbusBuf[2]);
					}
					else if (gMB_CMDRX.Addr >= VAR_ADDR_S && gMB_CMDRX.Addr <= VAR_ADDR_E) // Register address
					{
						if (gMB_CMDRX.Addr <= (VAR_TIME + 5) || gMB_CMDRX.Addr + gMB_CMDRX.Num >= (VAR_TIME + 0))
						{
							var[(VAR_TIME + 0) * 2 + 1] = calendar.w_year - 2000;
							var[(VAR_TIME + 1) * 2 + 1] = calendar.w_month;
							var[(VAR_TIME + 2) * 2 + 1] = calendar.w_date;
							var[(VAR_TIME + 3) * 2 + 1] = calendar.hour;
							var[(VAR_TIME + 4) * 2 + 1] = calendar.min;
							var[(VAR_TIME + 5) * 2 + 1] = calendar.sec;
						}

						Feedback_ModbusBuf[0] = Addr_ModBus_Sla;
						Feedback_ModbusBuf[1] = gMB_CMDRX.CMD;
						Feedback_ModbusBuf[2] = 2 * gMB_CMDRX.Num;
						for (i = 0; i < gMB_CMDRX.Num; i++)
						{
							Feedback_ModbusBuf[3 + 2 * i] = var[2 * (gMB_CMDRX.Addr + i)];
							Feedback_ModbusBuf[4 + 2 * i] = var[2 * (gMB_CMDRX.Addr + i) + 1];
						}
						LT_SendData_CRC_NoFrame(Feedback_ModbusBuf, 3 + Feedback_ModbusBuf[2]);
					}
					else
					{
						Feedback_ModbusBuf[0] = Addr_ModBus_Sla;
						Feedback_ModbusBuf[1] = gMB_CMDRX.CMD + 0x80;
						Feedback_ModbusBuf[2] = MB_ERR_ADDR;
						LT_SendData_CRC_NoFrame(Feedback_ModbusBuf, 3);
					}
				}
				else if (gMB_CMDRX.CMD == 0x06) // Write instruction response
				{
//		if(MB_SLA_Len>0)
//		{
//		printf("MB_SLA_Len %d   0x%x \r\n",MB_SLA_Len,gMB_CMDRX.Addr );
//		printf("Addr_ModBus_Sla %x  \r\n",Addr_ModBus_Sla);
//		for(uint8_t i=0;i<10;i++)printf(" %x ",gUsartRx.Buf[i]);
//		printf(" \r\n");
//		for(uint8_t i=0;i<10;i++)printf(" %x ",MB_SLA_RXBUF[i]);

//		printf(" \r\n");
//		printf(" \r\n");
//		}
					if (gMB_CMDRX.Addr < VAR_ADDR_S) // Variable address
					{
						var[2 * gMB_CMDRX.Addr] = MB_SLA_RXBUF[4];
						var[2 * gMB_CMDRX.Addr + 1] = MB_SLA_RXBUF[5];

						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(MB_SLA_RXBUF, 6);
					}
					else if (gMB_CMDRX.Addr >= VAR_ADDR_S && gMB_CMDRX.Addr <= VAR_ADDR_E) // Register address
					{
						var[2 * gMB_CMDRX.Addr] = MB_SLA_RXBUF[4];
						var[2 * gMB_CMDRX.Addr + 1] = MB_SLA_RXBUF[5];
						flag_MB_REG[gMB_CMDRX.Addr - VAR_ADDR_S] = 1;

						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(MB_SLA_RXBUF, 6);
					}
					else
					{
						Feedback_ModbusBuf[0] = Addr_ModBus_Sla;
						Feedback_ModbusBuf[1] = gMB_CMDRX.CMD + 0x80;
						Feedback_ModbusBuf[2] = MB_ERR_ADDR;
						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(Feedback_ModbusBuf, 3);
					}
				}
				else if (gMB_CMDRX.CMD == 0x10) // Write multiple instruction response
				{
					gMB_CMDRX.Num = MB_SLA_RXBUF[4] << 8 | MB_SLA_RXBUF[5];
					if (gMB_CMDRX.Addr < VAR_ADDR_S) // Variable address
					{
						// Save data
						for (i = 0; i < gMB_CMDRX.Num; i++)
						{
							var[2 * (gMB_CMDRX.Addr + i)] = MB_SLA_RXBUF[7 + 2 * i];
							var[2 * (gMB_CMDRX.Addr + i) + 1] = MB_SLA_RXBUF[8 + 2 * i];
						}
						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(MB_SLA_RXBUF, 6);
					}
					else if (gMB_CMDRX.Addr >= VAR_ADDR_S && gMB_CMDRX.Addr <= VAR_ADDR_E) // Register address
					{
						for (i = 0; i < gMB_CMDRX.Num; i++)
						{
							var[2 * (gMB_CMDRX.Addr + i)] = MB_SLA_RXBUF[7 + 2 * i];
							var[2 * (gMB_CMDRX.Addr + i) + 1] = MB_SLA_RXBUF[8 + 2 * i];
							flag_MB_REG[gMB_CMDRX.Addr - VAR_ADDR_S + i] = 1;
						}
						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(MB_SLA_RXBUF, 6);
					}
					else if (gMB_CMDRX.Addr >= VAR_CURVE_WS && gMB_CMDRX.Addr <= VAR_CURVE_WE) 
					{
						gMB_CMDRX.Num = MB_SLA_RXBUF[4] << 8 | MB_SLA_RXBUF[5];

						temp = gMB_CMDRX.Addr & 0xFFF;
						for (i = 0; i < CURVE_SIZE; i++) //Record how many groups of channels
						{
							if (temp & (1 << i))
								num++;
						}

						i = 0;
						j = 0;
						while (j < gMB_CMDRX.Num) // Store data to corresponding channels respectively
						{
							if (temp & (1 << i))
							{
								Curve_buff[i][Curve_count[i]] = MB_SLA_RXBUF[7 + 2 * j] << 8 | MB_SLA_RXBUF[8 + 2 * j];
								Curve_count[i]++;
								if (Curve_count[i] >= 1024)
								{
									Curve_count[i] = 0;
								}
								curve_update_flag[i] = 1;
								j++;
							}
							i++;
							if (i >= CURVE_SIZE)
								i = 0;
						}
						/* A consensus was reached that when multi-channel buffering data was triggered,
						if some channel data was missing, the missing part would be supplemented */
						if (gMB_CMDRX.Num % num)
						{
							for (i = 0; i < CURVE_SIZE; i++)
							{
								if (temp & (1 << i))
									cnt++;
								if (cnt > gMB_CMDRX.Num % num && (temp & (1 << i)))
								{
									if (Curve_count[i] != 0)
										Curve_buff[i][Curve_count[i]] = Curve_buff[i][Curve_count[i] - 1];
									else
										Curve_buff[i][0] = Curve_buff[i][1023];

									Curve_count[i]++;
									if (Curve_count[i] >= 1024)
									{
										Curve_count[i] = 0;
									}
								}
							}
						}
						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(MB_SLA_RXBUF, 6);
					}
					else if (gMB_CMDRX.Addr >= VAR_CURVE_CS && gMB_CMDRX.Addr <= VAR_CURVE_CE)
					{
						temp = gMB_CMDRX.Addr & 0xFFF;
						for (i = 0; i < CURVE_SIZE; i++) // Store data to corresponding channels respectively
						{
							if (temp & (1 << i))
							{
								for (j = 0; j < 1024; j++)
								{
									Curve_buff[i][j] = 0x00;
								}

								Curve_count[i] = 0;
								curve_update_flag[i] = 1;
							}
						}
						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(MB_SLA_RXBUF, 6);
					}
					else
					{
						Feedback_ModbusBuf[0] = Addr_ModBus_Sla;
						Feedback_ModbusBuf[1] = gMB_CMDRX.CMD + 0x80;
						Feedback_ModbusBuf[2] = MB_ERR_ADDR;
						if (MB_SLA_RXBUF[0] != 0)
							LT_SendData_CRC_NoFrame(Feedback_ModbusBuf, 3);
					}
				}
				else
				{
					Feedback_ModbusBuf[0] = Addr_ModBus_Sla;
					Feedback_ModbusBuf[1] = gMB_CMDRX.CMD + 0x80;
					Feedback_ModbusBuf[2] = MB_ERR_CMD;
					if (MB_SLA_RXBUF[0] != 0)
						LT_SendData_CRC_NoFrame(Feedback_ModbusBuf, 3);
				}
			}
			else
			{
			}
		}
		else // The sending and receiving nodes are inconsistent
		{
		}
	}

	if (pit_1ms_count2 >= 10)
	{
		pit_1ms_count2 = 0;

		if (Var_Count_num > 0)
			Var_Count_flag = 1;
		if(Pointer_Count_num >0)
			Pointer_Count_flag = 1;
		if (Gif_num > 0)
			Gif_flag = 1;
		if (Adj_loop > 0)
			Adj_time++;
		if (DioKb_flag == 1)
			Cusor_time++;
		if (AioKb_flag == 1)
			Cusor_time++;
		if (GBKioKb_flag == 1)
			Cusor_time++;
		if (Scroll_num > 0)
			Scroll_flag = 1;
		if (curve_num > 0)
			curve_flag = 1;
		if (Backlight_flag)
			Backlight_count++;
		if (buzzer_start)
		{
			buzzer_10ms_count++;
			if (buzzer_10ms_count >= 5)
			{
				touch_buzzer_close();
				buzzer_start = 0;
				buzzer_10ms_count = 0;
			}
		}
#if (LT_TOUCH_FLAG == 1)
		if (RTP_read_Flag == 1)
		{
			RTP_time++;
			if (RTP_time > 5)
			{
				RTP_read_Flag = 0;
				RTP_time = 0;
			}
		}
#endif
		page_refreshing++;
		if(ctp_press_t < 0xFF) ctp_press_t++;

#if LT_ENCODER
	if (SW_flag4 != 2 || SW_flag4 != 3)
		key_time++;

	if (Encoder_read_Flag == 1)
	{
		Backlight_count = 0;
		if (Encoder_type == 0)
		{
			Encoder_time++;
			if (Encoder_time > 1)
			{
				if (Read_Encoder_key == 1)
				{
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
					SW_flag = 1;
				}
			}
		}
		else if (Encoder_type == 4)
		{
			Encoder_time++;
			if (Encoder_time > 1)
			{
				if (SW_flag4 == 1)
				{
					SW_flag4 = 2;
					SW_flag = 3;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
				if (Encoder_time > (Long_press * 5) && SW_flag4 != 3)
				{
					SW_flag4 = 3;
					SW_flag = 4;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
				if (Read_Encoder_key == 1)
				{
					SW_flag4 = 1;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
			}
		}
	}
	if (SW_flag4 == 1)
	{
		Encoder_time++;
		if (Encoder_time > Double_click_interval * 5) // Double-click valid time exceeded
		{
			SW_flag4 = 1;
			SW_flag = 2;
		}
	}
	if (Enc1)
	{
		Encoder_Freetime++;
		if (Encoder_Freetime > 100)
		{
			Encoder_Freetime = 0;
			knob_dir = 0;
			Enc1 = 0;
		}
	}
#endif
	}
}

#endif

#if (UARTBUS_OPTION == 2)
void PIT1_Init(void)
{
	NVIC_Init(1, 0, PIT1_IRQn, 2);

	PIT1->PCSR &= (~PCSR_EN);
	PIT1->PCSR = (3 << 8) | PCSR_OVW | PCSR_PIE | PCSR_RLD | PCSR_PDBG; // 75mhz/8  Interrupt mode
	PIT1->PMR = 9375;													// 1ms
	PIT1->PCSR |= PCSR_EN;
}

volatile uint32_t pit_1ms_count = 0;
volatile uint8_t uart_flag = 0;

volatile uint32_t pit_1ms_count2 = 0;
volatile uint32_t tx_10ms_cnt = 0;
volatile uint8_t tx_500ms_flag = 0;

volatile uint32_t pit_1ms_count3 = 0;
volatile uint8_t timeout_flag = 0;
volatile uint8_t MB_SLA_RXBUF[256] = {0};
volatile uint16_t MB_SLA_Len = 0;
void PIT1_IRQHandler(void)
{
	uint16_t CRC_MCU = 0, CRC_RX = 0;
	uint16_t i = 0;

	PIT1->PCSR |= PCSR_PIF; // clear PIF interrupt flag
	pit_1ms_count++;
	pit_1ms_count2++;
	pit_1ms_count3++;

	if (pit_1ms_count >= 5 && uart_flag) // If the serial port is detected for 5ms and no data is received, the reception is completed
	{
		pit_1ms_count = 0;
		uart_flag = 0;
		gUsartRx.Flag = 1;

		MB_SLA_Len = gUsartRx.Count;
		memcpy(MB_SLA_RXBUF, gUsartRx.Buf, MB_SLA_Len);
		gUsartRx.Count = 0;

		CRC_MCU = (MB_SLA_RXBUF[MB_SLA_Len - 1] << 8) + (MB_SLA_RXBUF[MB_SLA_Len - 2] & 0x00FF);
		CRC_RX = CRC16(MB_SLA_RXBUF, MB_SLA_Len - 2);

		if (CRC_MCU == CRC_RX) // CRC check is correct
		{
			if (MB_SLA_RXBUF[0] == gUsartTx.Sla_Add) // The sending and receiving nodes are consistent
			{
				pit_1ms_count3 = 0;
				timeout_flag = 0;
				
				if (MB_SLA_RXBUF[1] == gUsartTx.CMD)
				{
					if (gUsartTx.CMD == 0x01 || gUsartTx.CMD == 0x02) // Read command response	
					{
						if ( (gUsartTx.Len%8==0 && gUsartTx.Len/8==MB_SLA_RXBUF[2]) || (gUsartTx.Len%8!=0 && gUsartTx.Len/8+1==MB_SLA_RXBUF[2]) ) // Judge whether the number of read bytes sent and responded is the same
						{
							uint16_t addr_temp = gUsartTx.Var_Add;
							Var_AddrRemapping(&addr_temp); 

							for (i = 0; i < gUsartTx.Len; i++) // Save variables
							{
								//For debug
//								if( MB_SLA_RXBUF[3+i/8]&(1<<(i%8)) )	
//									var[2 * (gUsartTx.Var_Add) + (gUsartTx.REG_Add%8+i)/8] |= 1<<( (gUsartTx.REG_Add+i)%8);
//								else
//									var[2 * (gUsartTx.Var_Add) + (gUsartTx.REG_Add%8+i)/8] &=~(1<<( (gUsartTx.REG_Add+i)%8) );

								if( (gUsartTx.REG_Add+i)%16 < 8)	
								{
									if( MB_SLA_RXBUF[3+i/8]&(1<<(i%8)) )	
										var[2 * (addr_temp + (gUsartTx.REG_Add%8+i)/16)+1] |= 1<<( (gUsartTx.REG_Add+i)%8);
									else
										var[2 * (addr_temp + (gUsartTx.REG_Add%8+i)/16)+1] &=~(1<<( (gUsartTx.REG_Add+i)%8) );									
								}
								else
								{
									if( MB_SLA_RXBUF[3+i/8]&(1<<(i%8)) )	
										var[2 * (addr_temp + (gUsartTx.REG_Add%8+i)/16)] |= 1<<( (gUsartTx.REG_Add+i)%8);
									else
										var[2 * (addr_temp + (gUsartTx.REG_Add%8+i)/16)] &=~(1<<( (gUsartTx.REG_Add+i)%8) );									
								}								
							}

							/*   Customization
							if(Master_mode03_flag[0] == 1)	//?????Code_????????????��???????????????????????????????
							{
								var[0x2002*2]   = Master_mode03_Var[0];
								var[0x2002*2+1] = Master_mode03_Var[1];
							}
							if(Master_mode03_flag[1] == 1)	//?????Code_????????????��???????????????????????????????
							{
								var[0x2003*2]   = Master_mode03_Var[2];
								var[0x2003*2+1] = Master_mode03_Var[3];
							}
							*/

							if (gUsartTx.Mode == 0x02) // Data clearing of parameter pointer address in mode 2
							{
								var[2 * (gUsartTx.Opt_Add)] = 0;
								var[2 * (gUsartTx.Opt_Add) + 1] = 0;
							}
							else if (gUsartTx.Mode == 0x03) // Customized Code_ The flag bit corresponding to mode 3 is cleared
							{
								Master_mode03_flag[gUsartTx.Opt_Add] = 0;
							}
							tx_repeat_cnt = 0;
							tx_repeat_flag = 0; // The response will not need to be retransmitted, and the next command will be executed
						}
						else
						{
							tx_repeat_flag = 1;
						}
					}					
					else if (gUsartTx.CMD == 0x03 || gUsartTx.CMD == 0x04) // Read command response
					{
						if (gUsartTx.Len == MB_SLA_RXBUF[2] / 2) // Judge whether the number of read bytes sent and responded is the same
						{
							uint16_t addr_temp = gUsartTx.Var_Add;
							Var_AddrRemapping(&addr_temp); 

							for (i = 0; i < MB_SLA_RXBUF[2] / 2; i++) // Save variables
							{
								var[2 * (addr_temp) + 2 * i] = MB_SLA_RXBUF[3 + 2 * i];
								var[2 * (addr_temp) + 2 * i + 1] = MB_SLA_RXBUF[3 + 2 * i + 1];

								if (addr_temp >= VAR_ADDR_S && addr_temp <= VAR_ADDR_E) // Register address
								{
									flag_MB_REG[addr_temp - VAR_ADDR_S + i] = 1;
								}
							}

							/*   Customization
							//Solves the problem that the new value of the variable is overwritten by the old value when the variable is read and written at the same time
							if(Master_mode03_flag[0] == 1)
							{
								var[0x2002*2]   = Master_mode03_Var[0];
								var[0x2002*2+1] = Master_mode03_Var[1];
							}
							if(Master_mode03_flag[1] == 1)
							{
								var[0x2003*2]   = Master_mode03_Var[2];
								var[0x2003*2+1] = Master_mode03_Var[3];
							}
							*/

							if (gUsartTx.Mode == 0x02) // Data clearing of parameter pointer address in mode 2
							{
								var[2 * (gUsartTx.Opt_Add)] = 0;
								var[2 * (gUsartTx.Opt_Add) + 1] = 0;
							}
							else if (gUsartTx.Mode == 0x03) // Customized Code_ The flag bit corresponding to mode 3 is cleared
							{
								Master_mode03_flag[gUsartTx.Opt_Add] = 0;
							}
							tx_repeat_cnt = 0;
							tx_repeat_flag = 0; // The response will not need to be retransmitted, and the next command will be executed
						}
						else
						{
							tx_repeat_flag = 1;
						}
					}
					else if (gUsartTx.CMD == 0x05 || gUsartTx.CMD == 0x06) // Write instruction response
					{
						if (gUsartTx.REG_Add == (MB_SLA_RXBUF[2] << 8 | MB_SLA_RXBUF[3])) // Judge whether the register address of sending and responding is the same
						{
							if (gUsartTx.Mode == 0x02) // Data clearing of parameter pointer address in mode 2
							{
								var[2 * (gUsartTx.Opt_Add)] = 0;
								var[2 * (gUsartTx.Opt_Add) + 1] = 0;
							}
							else if (gUsartTx.Mode == 0x03) // Customized Code_ The flag bit corresponding to mode 3 is cleared
							{
								Master_mode03_flag[gUsartTx.Opt_Add] = 0;
							}
							tx_repeat_cnt = 0;
							tx_repeat_flag = 0; // The response will not need to be retransmitted, and the next command will be executed
						}
						else
						{
							tx_repeat_flag = 1;
						}
					}
					else if (gUsartTx.CMD == 0x0F || gUsartTx.CMD == 0x10) // Write multiple instruction response
					{
						if (gUsartTx.REG_Add == (MB_SLA_RXBUF[2] << 8 | MB_SLA_RXBUF[3])) // Judge whether the register address of sending and responding is the same
						{
							if (gUsartTx.Mode == 0x02) // Data clearing of parameter pointer address in mode 2
							{
								var[2 * (gUsartTx.Opt_Add)] = 0;
								var[2 * (gUsartTx.Opt_Add) + 1] = 0;
							}
							else if (gUsartTx.Mode == 0x03) // Customized Code_ The flag bit corresponding to mode 3 is cleared
							{
								Master_mode03_flag[gUsartTx.Opt_Add] = 0;
							}
							tx_repeat_cnt = 0;
							tx_repeat_flag = 0; // The response will not need to be retransmitted, and the next command will be executed
						}
						else
						{
							tx_repeat_flag = 1;
						}
					}
				}
				else // Resend if the answer is wrong
				{
					tx_repeat_flag = 1;
				}
			}
			else // Resend if the sending and receiving nodes are inconsistent
			{
				tx_repeat_flag = 1;
			}
		}
		else
		{
			tx_repeat_flag = 1; // Resend if CRC check error
		}
		if (gUsartTx.Sla_Add != 0)
			tx_en_flag = 1; // After receiving a frame of unicast mode slave response, it is allowed to send flag bit enable
	}

	if (pit_1ms_count3 > gUsartTx.Timeout && timeout_flag)
	{
		pit_1ms_count3 = 0;
		timeout_flag = 0;
		tx_repeat_flag = 1;
		tx_en_flag = 1; // After receiving a frame, allow to send flag bit enable
	}

	if (pit_1ms_count2 > 10)
	{
		pit_1ms_count2 = 0;

		tx_10ms_cnt++;
		if (tx_10ms_cnt > 50)
		{

			tx_500ms_flag = 1;
		}

		if (Var_Count_num > 0)
			Var_Count_flag = 1;
		if(Pointer_Count_num >0)
			Pointer_Count_flag = 1;
		if (Gif_num > 0)
			Gif_flag = 1;
		if (Adj_loop > 0)
			Adj_time++;
		if (DioKb_flag == 1)
			Cusor_time++;
		if (AioKb_flag == 1)
			Cusor_time++;
		if (GBKioKb_flag == 1)
			Cusor_time++;
		if (Scroll_num > 0)
			Scroll_flag = 1;
		if (curve_num > 0)
			curve_flag = 1;
		if (Backlight_flag)
			Backlight_count++;
		if (buzzer_start)
		{
			buzzer_10ms_count++;
			if (buzzer_10ms_count >= 5)
			{
				touch_buzzer_close();
				buzzer_start = 0;
				buzzer_10ms_count = 0;
			}
		}
#if (LT_TOUCH_FLAG == 1)
		if (RTP_read_Flag == 1)
		{
			RTP_time++;
			if (RTP_time > 5)
			{
				RTP_read_Flag = 0;
				RTP_time = 0;
			}
		}
#endif
		page_refreshing++;
		if(ctp_press_t < 0xFF) ctp_press_t++;

#if LT_ENCODER
	if (SW_flag4 != 2 || SW_flag4 != 3)
		key_time++;

	if (Encoder_read_Flag == 1)
	{
		Backlight_count = 0;
		if (Encoder_type == 0)
		{
			Encoder_time++;
			if (Encoder_time > 1)
			{
				if (Read_Encoder_key == 1)
				{
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
					SW_flag = 1;
				}
			}
		}
		else if (Encoder_type == 4)
		{
			Encoder_time++;
			if (Encoder_time > 1)
			{
				if (SW_flag4 == 1)
				{
					SW_flag4 = 2;
					SW_flag = 3;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
				if (Encoder_time > (Long_press * 5) && SW_flag4 != 3)
				{
					SW_flag4 = 3;
					SW_flag = 4;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
				if (Read_Encoder_key == 1)
				{
					SW_flag4 = 1;
					Encoder_read_Flag = 0;
					Encoder_time = 0;
					key_time = 0;
				}
			}
		}
	}
	if (SW_flag4 == 1)
	{
		Encoder_time++;
		if (Encoder_time > Double_click_interval * 5) // Double-click valid time exceeded
		{
			SW_flag4 = 1;
			SW_flag = 2;
		}
	}
	if (Enc1)
	{
		Encoder_Freetime++;
		if (Encoder_Freetime > 100)
		{
			Encoder_Freetime = 0;
			knob_dir = 0;
			Enc1 = 0;
		}
	}
#endif

	}
}
#endif

void PIT2_Init(uint16_t arr)
{
	NVIC_Init(2, 0, PIT2_IRQn, 2);

	PIT2->PCSR &= (~PCSR_EN);
	PIT2->PCSR = (0 << 8) | PCSR_OVW | PCSR_PIE | PCSR_RLD | PCSR_PDBG; // 75mhz/1  Interrupt mode
	PIT2->PMR = arr - 3;

	PIT2->PCSR &= ~PCSR_EN;
}

void PIT2_IRQHandler(void)
{
}

// Disable timer PIT1
void LT_UsartTimeDisable(void)
{
	PIT1->PCSR &= ~PCSR_PIE;
	PIT1->PCSR &= ~PCSR_EN;
}

// Enable timer PIT1
void LT_UsartTimeEnable(void)
{

	PIT1->PCSR |= PCSR_PIE;
	PIT1->PCSR |= PCSR_EN;
}

// Set Load value for timer PIT1
void LT_UsartTimeSetCounter(uint32_t Counter)
{
	PIT1->PMR = Counter;
	PIT1->PCSR |= PCSR_PIF;
	LT_UsartTimeEnable();
}
